package com.rowenci.iot_agriculture.util.iot;

import com.rowenci.iot_agriculture.bean.iot.Com;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.rowenci.iot_agriculture.util.iot.SerialUtil.outputStream;

/**
 * <p>
 * 模拟传感器 定时向串口发送指定信息
 * </p>
 *
 * @author rowenci
 * @date 2019/5/5 10:57
 */
public class SerialSendApplicationRunner implements ApplicationRunner {

    //指定串口名称
    String ILLUMINANCE = "COM2";  //光照强度传感器串口
    String TEMPHUMI = "COM4"; //温湿度传感器串口
    String READER = "COM4";  //阅读器串口

    //指定发送的数据
    String ILLUMINANCE_SEND = "60";
    String READER_SEND = "01008DA2D1FF";

    String HHHLTHTL = "00F000F0";   //温湿度都是24(F0)
    String TEMPHUMI_SEND = "EECC0A01010000" + HHHLTHTL + "0000FF";


    @Override
    public void run(ApplicationArguments args) throws Exception {

        //设置串口
        Com illuminanceCom = new Com();
        illuminanceCom.setComName(ILLUMINANCE);
        illuminanceCom.setBaudrate(115200);
        illuminanceCom.setSensor("illuminance_send");
        Com temphumiCom = new Com();
        temphumiCom.setComName(TEMPHUMI);
        temphumiCom.setBaudrate(115200);
        temphumiCom.setSensor("temphumi_send");
        /*Com readerCom = new Com();
        readerCom.setComName(READER);
        readerCom.setBaudrate(9600);
        readerCom.setSensor("reader_send");*/

        List<Com> list = new ArrayList<>();
        list.add(illuminanceCom);
        list.add(temphumiCom);
        //list.add(readerCom);

        while (true){
            for (int i = 0; i < list.size(); i++) {
                Com com = list.get(i);
                SerialUtil serial = new SerialUtil();
                int code = serial.startComPort(com);
                if (code == 1){
                    try {
                        switch (com.getSensor()){
                            case "illuminance_send":{
                                byte[] st = serial.hexStrToBinaryStr(ILLUMINANCE_SEND);
                                outputStream.write(st, 0, st.length);
                                serial.closeSerial();
                                break;
                            }
                            case "temphumi_send":{
                                byte[] st = serial.hexStrToBinaryStr(TEMPHUMI_SEND);
                                outputStream.write(st, 0, st.length);
                                serial.closeSerial();
                                break;
                            }
                            /*case "reader_send":{
                                System.out.println("发送串口reader_send打开成功");
                                byte[] st = serial.hexStrToBinaryStr(READER_SEND);
                                outputStream.write(st, 0, st.length);
                                System.out.println("reader_send发送成功");
                                serial.closeSerial();
                            }*/
                        }
                    }catch (IOException e){
                        serial.closeSerial();
                        e.printStackTrace();
                    }
                }
                Thread.sleep(5000);
            }
        }

    }

}
